package com.example.drug.jpa;


import com.example.common.entity.RxMain;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface RxMainRepository extends JpaRepository<RxMain, String> {
    @Query(value = "SELECT * FROM order_rx_main WHERE order_no_nvarchar =:orderNo and is_trial_pharm_ca_bit=true ORDER BY create_time_datetime DESC LIMIT 1",nativeQuery = true)
    RxMain findLastOrder(@Param("orderNo") String orderNo);

    @Query(value = "select count(1) from order_rx_main where chk_dc_id_bigint = ?1 and chk_status_tinyint = ?2",nativeQuery = true)
    Integer findCountByDcId(Long chkid, Integer chkstatus);

    @Query(value = "select count(1) from order_rx_main where chk_dc_id_bigint = ?1 and chk_status_tinyint in (1,2)",nativeQuery = true)
    Integer findAllCountByDcId(Long chkid);

    List<RxMain> findByOrderNOAndIsTrialPharmCa(String orderNO,Boolean isTrialPharmCa);

    @Query(value = "select b.* from order_main a"
            +" inner join order_rx_main b on a.order_no_nvarchar = b.order_no_nvarchar"
            +" where a.pid_bigint = :pid and a.a_type_int = 10 and a.oc_status_tinyint != 0 and a.system_status_tinyint = 2 AND b.is_trial_pharm_ca_bit=true"
            +" order by b.create_time_datetime desc"
            +" limit 1",nativeQuery = true)
    RxMain findByLastOne(@Param("pid") long pid);

    @Query(value = "select trial_pharm_signature_xml_nvarchar from order_rx_main where rx_order_no_nvarchar = ?1", nativeQuery = true)
    String getXml(String orderId);
}
